package cn.hx.malluser.controller;

import cn.hx.malluser.entity.UmsMember;
import cn.hx.malluser.entity.UmsMemberLevel;
import cn.hx.malluser.entity.UmsMemberReceiveAddress;
import cn.hx.malluser.service.UmsMemberLevelService;
import cn.hx.malluser.service.UmsMemberReceiveAddressService;
import cn.hx.malluser.service.UmsMemberService;
import cn.hx.malluser.utils.MD5Util;
import cn.hx.malluser.vo.UmsMemberVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.util.List;

@Controller
@MultipartConfig
public class UmsMemberController {
    @Autowired
    private UmsMemberService umsMemberService;

    @Autowired
    private UmsMemberLevelService levelService;

    @Autowired
    private UmsMemberReceiveAddressService receiveAddressService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/personal/{id}")
    public String findById(@PathVariable Integer id, ModelMap map) {
        UmsMemberVo vo = (UmsMemberVo) redisTemplate.opsForValue().get("user_" + id);
        map.addAttribute("userInfo", vo);
        return "personal";
    }

    /** 前往账号安全 */
    @GetMapping("/accountSafe/{id}")
    public String accountSafe(@PathVariable Long id, ModelMap map) {
        map.addAttribute("id", id);
        return "update-pwd";
    }

    /** 修改密码 */
    @PostMapping("/updatePwd")
    public String updatePwd(ModelMap map, Long id, String oldPwd, String newPwd, String reconfirm) {
        UmsMemberVo vo = (UmsMemberVo) redisTemplate.opsForValue().get("user_" + id);
        System.out.println("当前用户信息：" + vo);
//        UmsMember umsMember = umsMemberService.getById(id);
        if (vo != null) {
            String password = vo.getPassword();
            if (!password.equals(MD5Util.encrypt(oldPwd))) {
                map.addAttribute("tips", "密码错误");
                return "update-pwd";
            }else if (!newPwd.equals(reconfirm)) {
                map.addAttribute("tips", "两次密码不一致");
                return "update-pwd";
            }else if (password.equals(MD5Util.encrypt(newPwd))) {
                map.addAttribute("tips", "新密码与旧密码不能一致");
                return "update-pwd";
            }else {
                UpdateWrapper<UmsMember> wrapper = new UpdateWrapper<>();
                wrapper.set("password", MD5Util.encrypt(newPwd)).eq("id", id);
                umsMemberService.update(wrapper);
                // 删除redis中相应的用户数据
                redisTemplate.delete("user_" + id);
                return "redirect:/user/login";
            }
        }
        return "error";
    }

    /** 个人信息修改 */
    @PostMapping("/updateInfo")
    public String updateInfo(UmsMember umsMember, MultipartFile file) {
        System.out.println("修改信息==>" + umsMember);
        String originalFilename = file.getOriginalFilename();

        return "";
    }

    /** 地址簿管理 */
    @GetMapping("/addressBook/{memberId}")
    public String addressBook(@PathVariable Long memberId, ModelMap map) {
        QueryWrapper<UmsMemberReceiveAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        List<UmsMemberReceiveAddress> addressBook = receiveAddressService.list(queryWrapper);
        map.addAttribute("memberId", memberId);
        if (addressBook != null && addressBook.size() != 0) {
            map.addAttribute("addressBook", addressBook);
        }
        return "address-book";
    }

    /** 查询单个地址信息 */
    @GetMapping("/queryAddrById/{id}")
    @ResponseBody
    public UmsMemberReceiveAddress queryAddrById(@PathVariable Long id) {
        return receiveAddressService.getById(id);
    }

    /** 设置默认地址 */
    @PostMapping("/setDefaultAddress")
    @ResponseBody
    public boolean setDefaultAddress(UmsMemberReceiveAddress receiveAddress) {
        receiveAddress.setDefaultStatus(1);
        Long memberId = receiveAddress.getMemberId();
        UpdateWrapper<UmsMemberReceiveAddress> wrapper = new UpdateWrapper<UmsMemberReceiveAddress>()
                .set("default_status", 0).eq("member_id", memberId);
        boolean update = receiveAddressService.update(wrapper);
        if (!update) {
            return false;
        }
        return receiveAddressService.updateById(receiveAddress);
    }

    /** 保存新地址或修改地址 */
    @PostMapping("/addressSubmit")
    public String addressSubmit(UmsMemberReceiveAddress receiveAddress) {
        System.out.println(receiveAddress);
        Long id = receiveAddress.getId();
        Long memberId = receiveAddress.getMemberId();
        if (id != null) {
             receiveAddressService.updateById(receiveAddress);
        }else {
            receiveAddressService.save(receiveAddress);
        }
        return "redirect:/user/addressBook/" + memberId;
    }

    /** 删除地址 */
    @GetMapping("/delAddress/{id}")
    @ResponseBody
    public boolean delAddress(@PathVariable Long id) {
        boolean b = false;
        if (id != null) {
            b = receiveAddressService.removeById(id);
        }
        return b;
    }

}
